package com.suning.sawp.service.impl.task;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.dto.common.QueryResult;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.precharge.CommonRtn;
import com.suning.sawp.dto.task.RegionHandlerRtn;
import com.suning.sawp.dto.task.StaffInfo;
import com.suning.sawp.intf.dao.task.OrgRespmanDaoService;
import com.suning.sawp.intf.task.RegionHandlerService;
import com.suning.sawp.po.task.OrgRespman;
import com.suning.vgs.follow.remote.intf.StoreManRemoteService;
import com.suning.vgs.follow.remote.store.StoreManBaseDto;

@Service
public class RegionHandlerServiceImpl implements RegionHandlerService {

    @Resource
    StoreManRemoteService storeManRemoteService;

    @Resource
    OrgRespmanDaoService  orgRespmanDaoService;

    @Override
    public ReturnMsg<CommonRtn> saveRegionHandler(String regionCode, String regionName,
            String staffCode, String staffName) {
        ReturnMsg<CommonRtn> ret = new ReturnMsg<CommonRtn>();
        if (!checkStr(regionCode, regionName, staffCode, staffName)) {
            ret.setError(ErrorCodeConstants.MAIN_PUSH_ERR_E001, ErrorCodeConstants.MAIN_PUSH_ERR_MAP);
            return ret;
        }
        OrgRespman respMan = new OrgRespman();
        respMan.setOrgCode(regionCode);
        respMan.setOrgName(regionName);
        respMan.setOrgLevel(OrgRespman.Attributes.REGION_LEVEL);
        respMan.setStaffCode(staffCode);
        respMan.setStaffName(staffName);
        orgRespmanDaoService.insertOrgRespman(respMan);
        ret.setRetFlag(ReturnMsg.SUCCESS);
        return ret;
    }

    @Override
    public ReturnMsg<CommonRtn> updateStaffInfo(Integer Id, String staffCode, String staffName) {
        ReturnMsg<CommonRtn> ret = new ReturnMsg<CommonRtn>();
        if (Id != null && checkStr(staffCode, staffName)) {
            OrgRespman queryCon = new OrgRespman();
            queryCon.setStaffCode(staffCode);
            queryCon.setOrgLevel(OrgRespman.Attributes.REGION_LEVEL);
            int count = orgRespmanDaoService.queryOrgRespmanCountBycon(queryCon);
            if(count > 0){
                ret.setError(ErrorCodeConstants.MAIN_PUSH_ERR_E012, ErrorCodeConstants.MAIN_PUSH_ERR_MAP);
                return ret;
            }
            OrgRespman respMan = new OrgRespman();
            respMan.setStaffCode(staffCode);
            respMan.setStaffName(staffName);
            if (orgRespmanDaoService.updateOrgRespman(respMan, Id)) {
                ret.setRetFlag(ReturnMsg.SUCCESS);
            } else {
                ret.setError(ErrorCodeConstants.MAIN_PUSH_ERR_E005, ErrorCodeConstants.MAIN_PUSH_ERR_MAP);
            }
        } else {
            ret.setError(ErrorCodeConstants.MAIN_PUSH_ERR_E001, ErrorCodeConstants.MAIN_PUSH_ERR_MAP);
        }
        return ret;
    }

    @Override
    public ReturnMsg<StaffInfo> queryStaffName(String staffCode) {
        ReturnMsg<StaffInfo> ret = new ReturnMsg<StaffInfo>();
        if (StringUtils.isNotBlank(staffCode)) {
            com.suning.vgs.follow.remote.store.ReturnMsg<StoreManBaseDto> storeMan = storeManRemoteService.getStoreManInfoWhenLogin(staffCode);
            if (storeMan != null &&  ReturnMsg.SUCCESS.equals(storeMan.getRetFlag()) && StringUtils.isNotBlank(storeMan.getData().getStoreManName())) {
                ret.setRetFlag(ReturnMsg.SUCCESS);
                StaffInfo staff = new StaffInfo();
                staff.setStaffName(storeMan.getData().getStoreManName());
                ret.setData(staff);
            } else {
                ret.setError(ErrorCodeConstants.MAIN_PUSH_ERR_E006, ErrorCodeConstants.MAIN_PUSH_ERR_MAP);
            }
        } else {
            ret.setError(ErrorCodeConstants.MAIN_PUSH_ERR_E001, ErrorCodeConstants.MAIN_PUSH_ERR_MAP);
        }
        return ret;
    }

    @Override
    public ReturnMsg<QueryResult<RegionHandlerRtn>> queryRegionHandlerList(String regionCode,
            Integer pageSize, Integer pageNo) {
        ReturnMsg<QueryResult<RegionHandlerRtn>> ret = new ReturnMsg<QueryResult<RegionHandlerRtn>>();
        QueryResult<RegionHandlerRtn> result = new QueryResult<RegionHandlerRtn>();
        if(pageNo == null){
            pageNo = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }
        OrgRespman queryCon = new OrgRespman();
        queryCon.setOrgLevel(OrgRespman.Attributes.REGION_LEVEL);
        if(StringUtils.isNotBlank(regionCode)){
            queryCon.setOrgCode(regionCode);
        }
        int count = orgRespmanDaoService.queryOrgRespmanCountBycon(queryCon);
        List<OrgRespman> list = orgRespmanDaoService.queryOrgRespmanByCon(queryCon, pageNo, pageSize);
        List<RegionHandlerRtn> retList = new ArrayList<RegionHandlerRtn>();
        if(CollectionUtils.isNotEmpty(list)){
            for(OrgRespman org : list){
                RegionHandlerRtn rtn = new RegionHandlerRtn();
                rtn.setId(org.getId());
                rtn.setRegionCode(org.getOrgCode());
                rtn.setRegionName(org.getOrgName());
                rtn.setStaffCode(org.getStaffCode());
                rtn.setStaffName(org.getStaffName());
                retList.add(rtn);
            }
        }
        result.setTotalSize(count);
        result.setRecords(retList);
        ret.setRetFlag(ReturnMsg.SUCCESS);
        ret.setData(result);
        return ret;
    }

    @Override
    public String queryRegionHandlerByStaffCode(String staffCode) {
        if(StringUtils.isNotBlank(staffCode)){
            OrgRespman queryCon = new OrgRespman();
            queryCon.setOrgLevel(OrgRespman.Attributes.REGION_LEVEL);
            queryCon.setStaffCode(staffCode);
            List<OrgRespman> list = orgRespmanDaoService.queryOrgRespmanByCon(queryCon, null, null);
            if(CollectionUtils.isNotEmpty(list)){
                return list.get(0).getOrgCode();
            }
        }
        return null;
    }

    private boolean checkStr(String... args) {
        for (String s : args) {
            if (StringUtils.isBlank(s)) {
                return false;
            }
        }
        return true;
    }
}
